package com.yoda.mall.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.admin.service.MenuService;
import com.yoda.mall.model.entity.ums.Menu;
import com.yoda.mall.model.vo.MenuTreeVo;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 根据parentId分页查询菜单
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list/{parentId}")
    public R<PageResult<Menu>> list(@PathVariable Integer parentId,
                                    @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageResult<Menu> pageResult = menuService.list(parentId, pageNum, pageSize);
        return R.success(pageResult);
    }

    /**
     * 菜单信息树形结构返回
     *
     * @return
     */
    @GetMapping("treeList")
    public R<List<MenuTreeVo>> treeList() {
        List<MenuTreeVo> vo = menuService.treeList();
        return R.success(vo);
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @PostMapping("create")
    public R create(@RequestBody Menu menu) {
        menuService.create(menu);
        return R.success(null);
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    public R delete(@PathVariable Integer id) {
        menuService.delete(id);
        return R.success(null);
    }

    /**
     * 根据ID获取菜单详情
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Menu> info(@PathVariable Integer id){
        Menu menu = menuService.info(id);
        return R.success(menu);
    }

    /**
     * 修改菜单
     * @param menu
     * @param id
     * @return
     */
    @PostMapping("update/{id}")
    public R update(@PathVariable Integer id,@RequestBody Menu menu){
        menuService.update(id,menu);
        return  R.success(null);
    }

    /**
     * 显示菜单或者不显示菜单
     * @param hidden
     * @param id
     * @return
     */
    @PostMapping("updateHidden/{id}")
    public R updateHidden(Integer hidden,@PathVariable Integer id){
        menuService.updateHidden(id,hidden);
        return R.success(null);
    }
}
